<?php

class ModuleController extends GeanyController {
    
    // <editor-fold defaultstate="collapsed" desc="STATIC & PRIVATE VARIABLE DECLARATION">
    private $validator;
    const _FUNCTION_ID = 'module';
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CONSTRUCTOR">
  
    public function __construct(){
        parent::__construct();
        $this->loadDefaultValue();
        $this->loadServices();
        $this->validator = null;
    }
	
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="ACTION-HANDLER">
  
    public function index(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $data['Model'] = $this->mService->getList();
        return View::make('module/index', $data);
    }

    public function create(){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        $module = new Module();
        
        try{
            if(Input::get('submit')==null){
                return $this->createInputView($module);
            } else {
                $module = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($module);
                $newId = $this->mService->insertObject($module);
                if($newId!=null){
                    Return Redirect::to('module/detail/'.$newId);
                }
                return $this->createInputView($module);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($module);
        }
    }
    
    public function detail($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        try {
            $result = $this->mService->getObject($id);
            if($result==null) {
                PageData::addModelError("Module with Id [ ".$id." ] Not Found ");
                Return Redirect::to('module')->with('ModelState',PageData::getModelState());
            }
            $data['Model'] = $result;
            return View::make('module/detail', $data);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('module');
        }
    }
    
    public function edit($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }
        
        $module = new Module();
        
        try {
            if(Input::get('submit')==null){
                $module = $this->mService->getObject($id);
                return $this->createInputView($module);
            }
            else{
                $module = $this->bindData();
                if($this->validateInput(Input::all())==FALSE) return $this->createInputView($module);
                if($this->mService->updateObject($module,$module->getId())==true){
                    Return Redirect::to('module/detail/'.$module->getId());
                }
                return $this->createInputView($module);
            }
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            return $this->createInputView($module);
        }
    }

    public function delete($id=null){
        if (!$this->isLogin()){ return Redirect::to('login'); }
        if (!$this->isAllowAccess()){ return Redirect::to('forbidden'); }

        try {
            $this->mService->deleteObject($id);
            
            Return Redirect::to('module');
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('module');
        }
    }


    
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="PRIVATE-FUNCTION">
    
    private function loadComboIsEnabled($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $arr_data[true] = 'TRUE';
        $arr_data[false] = 'FALSE';
        
        $this->data['IsEnabledCombo'] = $arr_data;
    }
    
    private function loadComboIsShow($show_all = false){
        $arr_data = array();
        
        if ($show_all) $arr_data[''] = 'Pilih Nilai';
        $arr_data[true] = 'TRUE';
        $arr_data[false] = 'FALSE';
        
        $this->data['IsShowCombo'] = $arr_data;
    }
    
    private function createInputView($module=null){
        $this->loadComboIsEnabled();
        $this->loadComboIsShow();
        $this->data['Model'] = $module;
        return View::make('module/input', $this->data);
    }
    
    private function bindData(){
        $data = Input::all();
        
        $module = New Module();
        $module->setId(is_null($data["id"]) ? strtolower($data["name"]) : $data["id"]);
        $module->setName($data["name"]);
        $module->setIcon($data["icon"]);
        $module->setSequence($data["sequence"]);
        $module->setIsShow($data["is_show"]);
        $module->setIsEnabled($data["is_enabled"]);

        return $module;
    }
 
    private function validateInput($data){

        if($data==null) return false;
        $rules = array(
            'name' => 'required',
            'sequence' => 'numeric|required',
            'icon' => '',
            'is_show' => 'boolean',
            'is_enabled' => 'boolean',
        );

        $messages = array(
            'name' => 'Yo dawg, this field aint long enough.',
            'sequence' => 'Yo dawg, this field aint long enough.',
            'url' => 'Yo dawg, this field aint long enough.',
            'icon' => 'Yo dawg, this field aint long enough.',
            'is_show' => 'Yo dawg, this field aint long enough.',
            'is_enabled' => 'Yo dawg, this field aint long enough.',
        );
        
        $this->validator = Validator::make($data, $rules,$messages);
        if($this->validator->fails()) PageData::assignValidatorToModelState ($this->validator);
        return $this->validator->passes();
    }
    
    private function loadDefaultValue(){
        $this->setFunctionId(self::_FUNCTION_ID);
        $this->page_data['_MODULE_NAME'] = $this->getFunctionName();
    }

    private function loadServices(){
        $this->mService = new ModuleService();
    }
    
    // </editor-fold>
    
}
?>